/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.user.domain.SysXbuser;
import com.xunyin.modules.user.service.SysXbuserService;
import com.xunyin.modules.user.service.dto.SysXbuserDto;
import com.xunyin.modules.user.service.dto.SysXbuserQueryCriteria;
import com.xunyin.modules.user.service.mapper.SysXbuserMapper;
import com.xunyin.utils.CommonConst;
import com.xunyin.utils.FileUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author admin
 * @date 2022-10-18
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "sysXbuser")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysXbuserServiceImpl extends BaseServiceImpl<SysXbuserMapper, SysXbuser> implements SysXbuserService {

    private final IGenerator generator;

    @Override
    public Integer saveLoginUser(String phoneNumber, String openId) {
        SysXbuser sysXbuser = new SysXbuser();
        sysXbuser.setUserAccount(phoneNumber);
        sysXbuser.setUserName(phoneNumber);
        sysXbuser.setIsUsed(CommonConst.ENABLE_FLAG);
        sysXbuser.setLastLoginTime(new Date());
        sysXbuser.setCreateTime(new Date());
        sysXbuser.setStatus(CommonConst.ENABLE_FLAG);
        sysXbuser.setUserType(1);
        sysXbuser.setUserStatus(1);
        sysXbuser.setOpenId(openId);
        Integer id = baseMapper.insert(sysXbuser);
        //this.save(sysXbuser);
        return id;
    }

    @Override
    public SysXbuser getLoginUser(String phoneNumber) {
        LambdaQueryWrapper<SysXbuser> wrapper = new LambdaQueryWrapper<>();
        //if(phoneNumber!=null){
        wrapper.eq(SysXbuser::getUserAccount, phoneNumber);
        // }
        SysXbuser sysXbuser = baseMapper.selectOne(wrapper);
        return sysXbuser;
    }

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(SysXbuserQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<SysXbuser> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<SysXbuser> queryAll(SysXbuserQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(SysXbuser.class, criteria));
    }


    @Override
    public void download(List<SysXbuserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysXbuserDto sysXbuser : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("车场id", sysXbuser.getLotId());
            map.put("账号", sysXbuser.getUserAccount());
            map.put("昵称", sysXbuser.getUserName());
            map.put("上次登录方式 1：微信登录，2：web登录", sysXbuser.getLastLoginType());
            map.put("上次登录时间", sysXbuser.getLastLoginTime());
            map.put("上次登录ip", sysXbuser.getLastLoginIp());
            map.put("角色code", sysXbuser.getRoleCode());
            map.put("组织id", sysXbuser.getOrgId());
            map.put("管理地点", sysXbuser.getRemark());
            map.put("启用标志 0：已禁用，1：已启用", sysXbuser.getIsUsed());
            map.put("创建日期", sysXbuser.getCreateTime());
            map.put("更新日期", sysXbuser.getUpdateTime());
            map.put("删除日期", sysXbuser.getDeleteTime());
            map.put("创建用户ID", sysXbuser.getCreateUserId());
            map.put("更新用户ID", sysXbuser.getUpdateUserId());
            map.put("状态", sysXbuser.getStatus());
            map.put("商户id", sysXbuser.getBusinessId());
            map.put("用户类型 1：用户，2洗车员，3：其他", sysXbuser.getUserType());
            map.put("是否新用户（1：是2：否）", sysXbuser.getUserStatus());
            map.put("茶室id", sysXbuser.getTeahouseId());
            //map.put("二维码", sysXbuser.getManagementQrCode());
            map.put(" isDel", sysXbuser.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
